package wpmp.security.mgr.service;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import wcommons.lang.DateUtils;
import wcommons.lang.NumberUtils;
import wcommons.lang.quickbean.Message;
import wpmp.extjs3.data.Page;
import wpmp.security.mgr.base.BaseService;
import wpmp.security.mgr.dao.KeyValueDao;
import wpmp.security.mgr.entity.KeyValue;
import wpmp.security.mgr.utils.PageUtils;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 11:01:57 PM Mar 1, 2013
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class KeyValueService extends BaseService {

	// private static final Logger LOG =
	// LoggerFactory.getLogger(KeyValueService.class);

	// 需要更新的字段
	private static final String[] UPDATE_PROPERTIES = { "name", "key", "value", "memo" };

	@Autowired
	private KeyValueDao dao;

	public KeyValue get(Long id) {
		KeyValue entity = dao.get(id);
		if (entity == null) {
			return entity;
		}
		KeyValue result = new KeyValue();
		BeanUtils.copyProperties(entity, result);
		return result;
	}

	public KeyValue get(String key) {
		KeyValue entity = dao.queryForObject("key", key);
		if (entity == null) {
			return entity;
		}
		KeyValue result = new KeyValue();
		BeanUtils.copyProperties(entity, result);
		return result;
	}

	public int getTotal() {
		return dao.queryForInt(Restrictions.eq("isVisible", Boolean.TRUE));
	}

	public List<KeyValue> get(Page page) {
		return dao.queryForList(PageUtils.setCriteria(
				dao.createCriteria(Restrictions.eq("isVisible", Boolean.TRUE)), page,
				Order.desc("updateTime")));
	}

	public Message validateKey(KeyValue entity) {
		Criteria criteria = dao.createCriteria(Restrictions.eq("key", entity.getKey()));
		if (NumberUtils.gtZero(entity.getId())) {
			criteria.add(Restrictions.ne("id", entity.getId()));
		}
		if (dao.queryForInt(criteria) > 0) {
			return Message.getErrorMessage("键[" + entity.getKey() + "]已存在！");
		}
		return Message.getDefaultMessage();
	}

	public Message insert(KeyValue entity) {
		Message message = validateKey(entity);
		if (message.notpass()) {
			return message;
		}

		final Timestamp currentTime = DateUtils.newTimestamp();

		entity.setIsVisible(Boolean.TRUE);
		entity.setCreateTime(currentTime);
		entity.setCreateUser(entity.getSysLoginName());
		entity.setUpdateTime(currentTime);
		entity.setUpdateUser(entity.getSysLoginName());
		dao.save(entity);

		message.putAttribute("id", entity.getId());
		return message;
	}

	public Message update(KeyValue entity) {
		Message message = validateKey(entity);
		if (message.notpass()) {
			return message;
		}

		KeyValue result = dao.get(entity.getId());

		wcommons.beanutils.BeanUtils.copyProperties(result, entity, UPDATE_PROPERTIES, false);
		result.setUpdateTime(DateUtils.newTimestamp());
		entity.setUpdateUser(entity.getSysLoginName());
		dao.save(result);

		return message;
	}

	public Message delete(KeyValue entity) {
		if (NumberUtils.gtZero(entity.getId())) {
			dao.delete(entity.getId());
		}
		else if (ArrayUtils.isNotEmpty(entity.getIds())) {
			dao.delete(entity.getIds());
		}
		else {
			return Message.getErrorMessage("请选择你要删除的记录");
		}

		return Message.getDefaultMessage();
	}
}
